package affichage;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import method.Tablier;
import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.GObject;
import no.geosoft.cc.graphics.GPosition;
import no.geosoft.cc.graphics.GSegment;
import no.geosoft.cc.graphics.GStyle;
import no.geosoft.cc.graphics.GText;

public class Board extends GObject {

    private static final int ech = 30;
    private static final int cote = 30;
    private List<GSegment> states;
    private GStyle[] stateStyle;
    private final GStyle textStyle;
    private GText[][] gtext;
    private final int max;
    private final int n;
    private final Tablier tablier;

    public Board(TablierGraph tabgraph) {
        super();
        this.max = tabgraph.max;
        this.n = tabgraph.n;
        this.tablier = tabgraph.tablier;
        stateStyle = new GStyle[ max + 1 ];
        for (int s = 0 ; s < max + 1 ; s++) {
            stateStyle[s] = new GStyle();
            Color color = colorF3(s);
            stateStyle[s].setForegroundColor(color);
            stateStyle[s].setBackgroundColor(color);
        }
        states = new ArrayList<GSegment>();
        textStyle = new GStyle();
        textStyle.setForegroundColor(new Color(0, 0, 0));
        textStyle.setFont(new Font("Dialog", Font.BOLD, 10));
        gtext = new GText[ n + 1 ][ n + 1 ];
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                gtext[i][j] = new GText(String.valueOf(tablier.getMap().get(tablier.getTab()[i][j])));
                gtext[i][j].setStyle(textStyle);
                gtext[i][j].setPositionHint(GPosition.SOUTH);
            }
        }
    }

    private Color colorF1(int s) {
        return new Color(0, 0, (200 * s) / 7 + 55);
    }

    private Color colorF3(int s) {
        int val = 255 * s / max;
        return new Color(val, val, val);
    }

    private Color colorF2(int s) {
        switch (s) {
            case 0:
                return Color.GRAY;
            case 1:
                return Color.CYAN;
            case 2:
                return Color.LIGHT_GRAY;
            case 3:
                return Color.YELLOW;
            case 4:
                return Color.ORANGE;
            case 5:
                return Color.RED;
            case 6:
                return Color.GREEN;
            case 7:
                return Color.MAGENTA;
            default:
                new Exception("illegal argument");
                return null;
        }
    }

    @Override
    public void draw() {
        for (int j = 1 ; j <= n ; j++) {
            for (int i = 1 ; i <= n ; i++) {
                GSegment etat = new GSegment();
                states.add(etat);
                addSegment(etat);
                etat.setStyle(stateStyle[tablier.getMap().get(tablier.getTab()[i][j])]);
                etat.setGeometry(Geometry.createRectangle(i * ech, j * ech, cote, cote));
                etat.addText(gtext[i][j]);
            }
        }
    }
}
